﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace PoliticalClassifier.Support
{
    /// <summary>
    /// CandidateResultsCollection contains the set of candidate simulation results.
    /// </summary>
    public sealed class CandidateResultsCollection : IEnumerable<ICandidateResults>
    {
        #region Construction
        /// <summary>
        /// Constructs the collection with a given list of candidate results.
        /// </summary>
        /// <param name="candidateResults">The candidate results to store in the collection.</param>
        public CandidateResultsCollection(List<ICandidateResults> candidateResults)
        {
            CandidateResults = candidateResults;
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets a strongly-typed enumerator over the list of candidate results.
        /// </summary>
        /// <returns>The strongly-typed enumerator.</returns>
        public IEnumerator<ICandidateResults> GetEnumerator()
        {
            return CandidateResults.GetEnumerator();
        }

        /// <summary>
        /// Gets weakly-typed enumerator over the list of candidate results.
        /// </summary>
        /// <returns>The weakly-typed enumerator.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)CandidateResults).GetEnumerator();
        }

        /// <summary>
        /// Gets a strongly-typed enumerator over the candidate results ordered in 
        /// ascending order by number of votes.
        /// </summary>
        /// <returns>The strongly-typed enumerator.</returns>
        public IEnumerable<ICandidateResults> SortedAscendingByNumberOfVotes()
        {
            return CandidateResults.OrderBy(results => results.NumberOfVotes);
        }

        /// <summary>
        /// Gets a strongly-typed enumerator over the candidate results ordered in 
        /// descending order by number of votes.
        /// </summary>
        /// <returns>The strongly-typed enumerator.</returns>
        public IEnumerable<ICandidateResults> SortedDescendingByNumberOfVotes()
        {
            return CandidateResults.OrderByDescending(results => results.NumberOfVotes);
        }

        /// <summary>
        /// Gets a strongly-typed enumerator over the candidate results ordered in 
        /// ascending order by distance from median.
        /// </summary>
        /// <returns>The strongly-typed enumerator.</returns>
        public IEnumerable<ICandidateResults> SortedAscendingByDistanceFromMedian()
        {
            return CandidateResults.OrderBy(results => results.DistanceFromMedian);
        }

        /// <summary>
        /// Gets a strongly-typed enumerator over the candidate results ordered in 
        /// descending order by distance from median.
        /// </summary>
        /// <returns>The strongly-typed enumerator.</returns>
        public IEnumerable<ICandidateResults> SortedDescendingByDistanceFromMedian()
        {
            return CandidateResults.OrderByDescending(results => results.DistanceFromMedian);
        }
        #endregion

        #region Private Properties
        /// <summary>
        /// The array of candidate results.
        /// </summary>
        private List<ICandidateResults> CandidateResults { get; set; }
        #endregion
    }
}
